package com.li;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.StringTemplateResolver;

public class Main {
    public static void main(String[] args) throws IOException {
        // 模板解析
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        // 设置模板类型
        templateResolver.setCharacterEncoding("UTF-8"); // 中文编码集
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setPrefix("/template/");
        templateResolver.setSuffix(".html");
        // 定义模板引擎
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        // 定义数据类型
        Context context = new Context();
        context.setVariable("name", "李司");
        // 渲染模板
        System.out.println(templateEngine.process("index", context));
        // 输出到文件流
        File file = new File("c:\\Users\\11\\Downloads", "index.html");
        Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        templateEngine.process("index", context, writer);
    }

    
    public static void processString() {
        // 1. 定义、设置模板解析器
        StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();
        // 设置模板类型为 TEXT
        stringTemplateResolver.setTemplateMode(TemplateMode.TEXT);

        // 2. 定义模板引擎
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(stringTemplateResolver);

        // 3. 定义数据模型
        Context context = new Context();
        context.setVariable("username", "张三");

        // 4. 渲染模板并输出到控制台
        System.out.println(templateEngine.process("[(${username})]", context));
    }

}